﻿using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Kinect;
using System.Threading;

namespace WpfKinectUserControls
{
    public partial class KinectTiltControl : UserControl
    {
        private int valNDU = 0;
        public int ValNDU
        {
            get { return valNDU; }
            set
            {
                valNDU = value;
                if (CurrentKinectSensor != null && CurrentKinectSensor.Status == KinectStatus.Connected)
                {
                    try
                    {
                        CurrentKinectSensor.ElevationAngle = valNDU;
                    }
                    catch
                    {
                        try
                        {
                            CurrentKinectSensor.ElevationAngle = valNDU;
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }
        public static readonly DependencyProperty MinElevationAngleProperty =
            DependencyProperty.Register("MinElevationAngle", typeof(int),
            typeof(KinectTiltControl), new FrameworkPropertyMetadata(int.MinValue));
        public static readonly DependencyProperty MaxElevationAngleProperty =
            DependencyProperty.Register("MaxElevationAngle", typeof(int),
            typeof(KinectTiltControl), new FrameworkPropertyMetadata(int.MinValue));
        public static readonly DependencyProperty ElevationAngleProperty =
            DependencyProperty.Register("ElevationAngle", typeof(int),
            typeof(KinectTiltControl), new FrameworkPropertyMetadata(int.MinValue));
        public static readonly DependencyProperty CurrentKinectSensorProperty =
            DependencyProperty.Register("CurrentKinectSensor", typeof(KinectSensor),
            typeof(KinectTiltControl), new PropertyMetadata(new PropertyChangedCallback(OnCurrentKinectSensorChanged)));
        private static void OnCurrentKinectSensorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var value = e.NewValue;
            if (value != null && ((KinectSensor)value).Status == KinectStatus.Connected)
            {
                ((KinectTiltControl)d).ElevationAngle = ((KinectSensor)value).ElevationAngle;
            }
            ((KinectTiltControl)d).IsEnabled = value != null;
        }
        public int MinElevationAngle
        {
            get { return (int)GetValue(MinElevationAngleProperty); }
            set { SetValue(MinElevationAngleProperty, value); }
        }
        public int MaxElevationAngle
        {
            get { return (int)GetValue(MaxElevationAngleProperty); }
            set { SetValue(MaxElevationAngleProperty, value); }
        }
        public int ElevationAngle
        {
            get { return (int)GetValue(ElevationAngleProperty); }
            set { SetValue(ElevationAngleProperty, value); }
        }
        public KinectSensor CurrentKinectSensor
        {
            get { return (KinectSensor)GetValue(CurrentKinectSensorProperty); }
            set { SetValue(CurrentKinectSensorProperty, value); }
        }
        public KinectTiltControl()
        {
            InitializeComponent();
        }
        private void uc_Loaded(object sender, RoutedEventArgs e)
        {
            this.IsEnabled = false;
        }
    }
}
